package org.jeecg.modules.payment.strategy;

import com.fasterxml.jackson.databind.JsonNode;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.payment.bo.PaymentParams;
import org.jeecg.modules.payment.bo.RefundParams;
import org.jeecg.modules.payment.enums.PayChannelEnums;
import org.jeecg.modules.payment.entity.PaymentConfig;
import org.jeecg.modules.payment.strategy.impl.ICBCPaymentService;
import org.jeecg.modules.payment.strategy.impl.WechatPaymentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author by sean
 * @desc:
 * @since 2024-08-31 星期六 13:52:45
 */
public class PaymentContext {

    private IPaymentService paymentService;

    public PaymentContext(PaymentConfig paymentConfig){
        if(PayChannelEnums.WECHAT_CHANNEL.getValue().equals(paymentConfig.getChannelCode())){
            this.paymentService = new WechatPaymentService(paymentConfig.getConfigContent());
        }
        if(PayChannelEnums.ICBC_CHANNEL.getValue().equals(paymentConfig.getChannelCode())){
            this.paymentService = new ICBCPaymentService(paymentConfig.getConfigContent());
        }
    }


    public JsonNode payment(PaymentParams params) throws JeecgBootException {
        return paymentService.payment(params);
    }

    public JsonNode payCallBack(HttpServletRequest request, String payWay, String tenantId) throws JeecgBootException {
        return paymentService.payCallBack(request, payWay, tenantId);
    }

    public JsonNode refund(RefundParams params) throws JeecgBootException{
        return paymentService.refund(params);
    }

    public JsonNode refundCallBack(JsonNode params) throws JeecgBootException {
        return paymentService.refundCallBack(params);
    }

    public void callbackResponse(HttpServletResponse response, JsonNode params, boolean result) {
        paymentService.callbackResponse(response, params, result);

    }
}
